﻿using System;
using System.Linq;
using System.Windows.Forms;
using QuanLyThongTinCoSo.Biz;
using QuanLyThongTinCoSo.Common;
using QuanLyThongTinCoSo.Entity;

namespace QuanLyThongTinCoSo.Forms.Settings
{
    public partial class NewUser : Form
    {
        private int id;

        private string userPassword;

        public NewUser()
        {
            InitializeComponent();
        }

        public NewUser(int id)
        {
            InitializeComponent();

            this.id = id;
        }

        private void SaveItem(object sender, EventArgs e)
        {
            if (Validate())
            {
                string message;

                var context = new Entities();

                var biz = new UserBiz(context);
                var roleBiz = new RoleBiz(context);

                if(id > 0)
                {
                    var item = (User)biz.GetItemById(id);

                    item.Roles.Clear();
                    foreach (Role role in cblRoles.CheckedItems)
                    {
                        item.Roles.Add(roleBiz.GetRoleByName(role.RoleName));
                    }
                    
                    biz.Update(item);

                    message = Constants.Messages.UpdateCompleted;
                }
                else
                {
                    var item = new User()
                    {
                        Username = textAccount.Text.Trim(),
                        FullName = textHoVaTen.Text.Trim(),
                        Password = Utilities.EncodePassword(Constants.DefaultPassword)
                    };

                    foreach (Role role in cblRoles.CheckedItems)
                    {
                        item.Roles.Add(roleBiz.GetRoleByName(role.RoleName));
                    }

                    biz.Insert(item);
                    message = Constants.Messages.InsertCompleted;
                }

                MessageBox.Show(message);
                Utilities.CloseForm(this, new ViewUser());
            }
        }

        private void Cancel(object sender, EventArgs e)
        {
            //Utilities.CloseForm(this, new ViewNguoiSuDung());
        }

        private new bool Validate()
        {
            bool hasError = true;
            bool isFocused = false;
            errorProvider.Clear();

            if (string.IsNullOrEmpty(textAccount.Text))
            {
                errorProvider.SetError(textAccount, Constants.Messages.RequireMessage);
                hasError = false;

                textAccount.Focus();
                isFocused = true;
            }

            if (string.IsNullOrEmpty(textHoVaTen.Text))
            {
                errorProvider.SetError(textHoVaTen, Constants.Messages.RequireMessage);
                hasError = false;
                
                if (!isFocused)
                {
                    textHoVaTen.Focus();
                    isFocused = true;
                }
            }

            if (cblRoles.CheckedItems.Count == 0)
            {
                errorProvider.SetError(cblRoles, Constants.Messages.RequireMessage);
                hasError = false;

                if (!isFocused)
                {
                    cblRoles.Focus();
                }
            }

            return hasError;
        }

        private void FormLoad(object sender, EventArgs e)
        {
            var parent = MdiParent as MDI;
            if (parent != null)
            {
                parent.Status.Text = string.Empty;
            }

            var context = new Entities();

            cblRoles.Items.Clear();

            LoadRoles(context);

            if(id > 0)
            {
                var biz = new UserBiz(context);
                var item = (User)biz.GetItemById(id);

                textAccount.Text = Convert.ToString(item.Username);
                textHoVaTen.Text = Convert.ToString(item.FullName);

                cblRoles.ClearSelected();

                foreach (var role in item.Roles)
                {
                    int index = cblRoles.FindStringExact(role.RoleName);
                    if(index > -1)
                    {
                        cblRoles.SetItemChecked(index, true);
                    }
                }

                buttonReset.Visible = true;
            }

            textAccount.Focus();
        }

        private void LoadRoles(Entities context)
        {
            var biz = new RoleBiz(context);
            foreach(var role in biz.DbContext.Roles)
            {
                cblRoles.Items.Add(role);
            }
        }

        private void ResetPassword(object sender, EventArgs e)
        {
            
        }
    }
}
